﻿/*
* UserManage.js
* zhouh@ZD_20160324
*/
$(function () {
    //announcement
    var divMsg = new jSharp.popUpDiv('divMsg');
    var divAdd = new jSharp.popUpDiv('divAdd');
    var txtAccount = $('#txtAccount');
    var txtpwd = $('#txtPwd1');
    var txtpwd2 = $('#txtPwd2');

    //function
    function addUser() {
        var level = $('.level').find('option:selected').val();
        var account = txtAccount.val();
        var pwd = txtpwd.val();
        var pwd2 = txtpwd2.val();
        if (account == '' || account == undefined) {
            tips('用户名不能为空！');
            return;
        }
        if (pwd == '' || pwd == undefined) {
            tips('密码不能为空！');
            return;
        }
        if (pwd2 != pwd) {
            tips('两次输入的密码不一致！');
            return;
        }
        if (!filterString(account, 50)) {
            tips('账号长度超过限制或含有非法字符！');
            return;
        }
        if (!filterString(pwd, 20)) {
            tips('密码长度超过限制或含有非法字符!');
            return;
        }
        $.post('../Common/AjaxHandler.aspx', {
            handler: 'isUserExist',
            account: account
        }, function (data) {
            if (data == 'true') {
                tips('该账号已经存在，请重新输入！');
                return;
            }
            else if (data == 'false') {
                $.post('../Common/AjaxHandler.aspx', {
                    handler: 'addUser',
                    account: account,
                    pwd: pwd,
                    level: level
                }, function (d) {
                    if (d == 'OK') {
                        tips('新增用户成功！');
                        txtAccount = '';
                        pwd = '';
                        pwd2 = '';
                        location.reload();
                    }
                    else {
                        tips('新增失败！');
                        console.log(d);
                    }
                })
            }
            else console.log(data);
        })
    }
    //Xss反注入

    function filterString(s, maxLen) {
        if (s.length > maxLen) return false;
        s = s.replace(/ /g, '');
        var param = new Array('<', '%3c', '>', '%3e', 'script', 'document', 'eval', '/', '%2f', '//');
        for (var i = 0; i < param.length; i++) {
            var reg = new RegExp(param[i]);
            if (reg.test(s)) return false;
        }
        return true;
    }
    function tips(s) { //消息提示
        divMsg.Show();
        $('#msgContent').html(s);
    }
    //handler

    $('#btnAdd').click(function () {
        addUser();
    })
    $('#hideMsg').click(function () {
        divMsg.Close();
    })
    $('#hideAdd').click(function () {
        divAdd.Close();
    })
    $('#btnAddUser').click(function () {
        divAdd.Show();
    })
    //init
    txtAccount.val("");
    txtpwd.val("");
} ())
